/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lenovo
 * Date: 2025-03-15
 * Time: 9:39
 */
public class Student {
    // 私有字段
    private String name;
    private int age;
    private boolean active;

    // 构造方法
    public Student(String name, int age, boolean active) {
        this.name = name;
        this.age = age;
        this.active = active;
    }

    // Getter 方法
    public String getName() {
        return this.name;
    }

    public int getAge() {
        return this.age;
    }

    public boolean isActive() {
        return this.active;
    }

    // Setter 方法
    public void setName(String name) {
        if (name != null && !name.isEmpty()) { // 添加逻辑验证
            this.name = name;
        } else {
            System.out.println("Invalid name!");
        }
    }

    public void setAge(int age) {
        if (age > 0 && age <= 100) { // 添加逻辑验证
            this.age = age;
        } else {
            System.out.println("Invalid age!");
        }
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    // 实例方法：显示学生信息
    public void displayInfo() {
        System.out.println("Name: " + this.name);
        System.out.println("Age: " + this.age);
        System.out.println("Active: " + this.active);
    }

    public static void main(String[] args) {
        // 创建 Student 对象
        Student student = new Student("Alice", 20, true);

        // 使用 Getter 方法获取字段值
        System.out.println("Name: " + student.getName()); // 输出: Name: Alice
        System.out.println("Age: " + student.getAge());   // 输出: Age: 20
        System.out.println("Active: " + student.isActive()); // 输出: Active: true

        // 使用 Setter 方法修改字段值
        student.setName("Bob");
        student.setAge(21);
        student.setActive(false);

        // 显示更新后的学生信息
        System.out.println("\nUpdated Student Information:");
        student.displayInfo();
    }
}

/*
运行：
        Name: Alice
        Age: 20
        Active: true

        Updated Student Information:
        Name: Bob
        Age: 21
        Active: false

*/
